<?php

class Admin_Table_Foods extends Apk_Db_Table
{
	protected $_name      = 'foods';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title', 'categories.menu_name_ru', 'public_date', 'is_active', 'is_top'))
    		  ->joinLeft('categories', 'categories.id = foods.id_cat', array('cat_name' => 'menu_name_ru'))
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
				case 'cats':
					if(count($v) > 0)
					$sel->where('id_cat in (?)', $v);
					break;
				case 'parent_id':
					if(count($v) > 0)
					$sel->where('categories.parent_id = ?', $v);
					break;
				case 'is_top':
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
					break;
				case 'ids':
					if(count($v) > 0 && is_array($v)) $sel->where($this->_name.'.id not in (?)', $v);
					break;
				case 's':
					if(trim($v) != '')
					{
						$sel->where($this->_name.'.title like \'%'.$v.'%\' or '.$this->_name.'.desc like \'%'.$v.'%\'  ');
						//$sel->group($this->_name.'.id');
					}
					break;
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);
			}
		}


		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.title');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }


    //*******************
    // insert/update/delete

	public function insert(array $data, $user_id)
    {
    	$data['public_date'] = time();
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;

        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	if(!isset($data['is_active'])) $data['is_active'] = 0;
    	if(!isset($data['is_top'])) $data['is_top'] = 0;

		try{
			parent::update($data, 'id = '.$id);
		}catch(Exception $e)
		{
			echo $e->__toString();
			die();
		}

		return true;
    }

    public function update_all(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id;
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}


    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }



}
?>